<template>
  <el-dialog  draggable overflow :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')">

    <el-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="right">
          <el-row>
            <el-col :span="8">
              <el-form-item label="编号" prop="code">
                <el-input label="编号" placeholder="自动生成" v-model="form.code" readonly></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="11">
              <el-form-item label="单据日期" prop="billdate">
                <el-input type="date" label="单据日期" v-model="form.billdate" clearable></el-input>
              </el-form-item>
            </el-col> -->

            <el-col :span="11">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <sc-VxeTable ref="xGrid1" :gridEvts="gridEvents" :data="form.details" :gridOps="gridOps"
            :tablecode="'purchase_apply_save'">
          </sc-VxeTable>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>

  </el-dialog>
</template>

<script>
import tool from "@/utils/tool"

export default {
  emits: ["success", "closed"],
  components: {},
  data() {
    return {
      mode: "edit",
      gridEvents: {},
      gridOps: {
        height: 300,
        columns: [
          {
            field: 'material.name',
            event: {
              select: ({ e, row }) => {
                row['material']['spec'] = e.code;
              }
            }
          }, {
            field: 'auxiliary_quantity',
            event: {
              keyup: ({ row }) => {
                row['quantity'] = row['auxiliary_quantity'] * 25;
              }
            }
          }],
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        id: "",
        code: "",
        department_id: "",
        details: [],
        // billdate: tool.SYS_DATE()
      },
      rules: {
        // billdate: [{ required: true, message: "请输入型号规格" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {

  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;

      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          // this.form.details = this.$refs.xGrid1.xGrid.getTableData().tableData;
          this.form['department_id'] = tool.data.get("USER_INFO").department.id;

          var res = await this.$API.purchase.apply.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    async getItems(id) {
      var res = await this.$API.purchase.apply.list.get({ filter: [{ main_id: id }] });
      if (res.code == 200) {
        this.form.details = res.data.rows;
      }
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data.main);
      this.getItems(data.main.id);
    },
  },
};
</script>

<style></style>
